package mynote;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

//密码输入框提示信息显示
public class PwdFocusListener implements FocusListener {
    protected String tipText;
    protected JPasswordField pwdField;

    public PwdFocusListener(String tipText, JPasswordField pwdField) {
        this.tipText = tipText;
        this.pwdField = pwdField;
        this.pwdField.setEchoChar((char)0);
        this.pwdField.setText(tipText);
        this.pwdField.setForeground(Color.GRAY);
    }
    @Override
    public void focusGained(FocusEvent e) {
        if (String.valueOf(this.pwdField.getPassword()).equals(tipText)) {
            this.pwdField.setText("");
            this.pwdField.setEchoChar('\u2022');
            this.pwdField.setForeground(Color.BLACK);
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        if (String.valueOf(this.pwdField.getPassword()).equals("")) {
            this.pwdField.setText(tipText);
            this.pwdField.setEchoChar((char)0);
            this.pwdField.setForeground(Color.GRAY);
        }
    }
}
